package com.hshe.springmvcdemo.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author h
 * @since 2019-12-20 18 29
 * 数据统计
 * 切面。
 */
@Aspect
@Component
@Slf4j
public class DataLogsAop {

    /**
     * 切入点
     */
    @Pointcut("execution( * com.hshe.springmvcdemo.controller.TestController.*(..))")
    public void webLogs() {
    }


    @Around(value = "webLogs()", argNames = "joinPoint")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        log.info("切面");

        // 接口处理前
        String api = ((MethodSignature) (joinPoint.getSignature())).getMethod().getAnnotation(RequestMapping.class).value()[0];
        Object[] args = joinPoint.getArgs();

//        JSON.toJSONString(args)

        //放行
        try {
            joinPoint.proceed();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }


        return null;
    }


}
